package beautiful.butterfly.drds.data_exchange.transport.record.column;

import beautiful.butterfly.drds.data_exchange.exception.CommonErrorCode;
import beautiful.butterfly.drds.data_exchange.exception.DataExchangeException;
import beautiful.butterfly.drds.data_exchange.transport.record.column.column_cast.ColumnCast;
import org.apache.commons.lang3.ArrayUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;


public class BytesColumn extends Column
{

    public BytesColumn()
    {
        this(null);
    }

    public BytesColumn(byte[] bytes)
    {
        //底层类型是字节数组
        super(ArrayUtils.clone(bytes), ColumnType.BYTES, null == bytes ? 0
                : bytes.length);
    }


    @Override
    public Boolean asBoolean()
    {
        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT, "Bytes类型不能转为Boolean .");
    }

    @Override
    public Long asLong()
    {
        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT, "Bytes类型不能转为Long .");
    }


    @Override
    public Double asDouble()
    {
        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT, "Bytes类型不能转为Long .");
    }

    @Override
    public BigDecimal asBigDecimal()
    {
        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT, "Bytes类型不能转为BigDecimal .");
    }

    @Override
    public BigInteger asBigInteger()
    {
        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT, "Bytes类型不能转为BigInteger .");
    }

    @Override
    public String asString()
    {
        if (null == this.getObject())
        {
            return null;
        }

        try
        {
            return ColumnCast.bytes2String(this);
        } catch (Exception e)
        {
            throw DataExchangeException.asDataExchangeException(
                    CommonErrorCode.CONVERT_NOT_SUPPORT,
                    String.format("Bytes[%s]不能转为String .", this.toString()));
        }
    }

    @Override
    public Date asDate()
    {
        throw DataExchangeException.asDataExchangeException(
                CommonErrorCode.CONVERT_NOT_SUPPORT, "Bytes类型不能转为Date .");
    }


    @Override
    public byte[] asBytes()
    {
        if (null == this.getObject())
        {
            return null;
        }

        return (byte[]) this.getObject();
    }
}
